package com.lpf;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

    public static void main(String[] args) throws Exception {
        //1.初始化并配置Configuration对象
        Configuration configuration = new Configuration(Configuration.getVersion());
        //2.设置模板文件所在的目录
        configuration.setClassForTemplateLoading(Main.class, "/template");
        //3.设置字符集
        configuration.setDefaultEncoding("utf-8");
        //4.加载模板文件
        Template template = configuration.getTemplate("test.ftl");
        //5.创建数据模型
        Map<String, Object> result = createData();
        //6.创建Writer对象
        FileWriter writer = new FileWriter("D:/test.html");
        //7.输出数据模型到文件中
        template.process(result, writer);
        //8.关闭Writer对象
        writer.close();
    }

    private static Map<String, Object> createData() {
        Map<String, Object> map = new HashMap<>();
        List<Animal> animals = new ArrayList<>();
        animals.add(new Elephant("大象", 10000, "large"));
        animals.add(new Mouse("老鼠", 10, "little"));
        animals.add(new Python("大蟒", 100, "middle"));
        map.put("animals", animals);

        map.put("user", "freemarker-user");
        return map;
    }
}
